package com.hxzy.controller.admin;

import com.hxzy.dto.PageDTO;
import com.hxzy.entity.TbNewbeeMallCarousel;
import com.hxzy.service.TbNewbeeMallCarouselService;
import com.hxzy.vo.BootstrapTableVO;
import com.hxzy.vo.ResultEnum;
import com.hxzy.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;

@Controller
@RequestMapping(value = "/admin")
public class TbNewbeeMallCarouselController {
    @Autowired
    private TbNewbeeMallCarouselService tbNewbeeMallCarouselService;

    @GetMapping(value = "/carousels")
    public ModelAndView search(){
      ModelAndView mv=new ModelAndView();
      mv.setViewName("admin/carousels");

      mv.addObject("path","carousels");

      return mv;
    }

    @ResponseBody
    @GetMapping(value = "/carousels/data")
    private ResultVO ajaxDate(PageDTO pageDTO){
        BootstrapTableVO tableVO=this.tbNewbeeMallCarouselService.search(pageDTO);

        return ResultVO.success(tableVO);
    }

    @ResponseBody
    @PostMapping(value = "/carousels/save")
    public ResultVO addOrUpdate(@Valid TbNewbeeMallCarousel tbNewbeeMallCarousel, BindingResult bindingResult){

        if (bindingResult.getFieldErrorCount()>0){
            String msg = bindingResult.getFieldErrors().get(0).getDefaultMessage();
            ResultVO failed = ResultVO.failed(ResultEnum.PARAMS_FAILED);
            failed.setMessage(msg);
            return failed;
        }

        return this.tbNewbeeMallCarouselService.addOrUpdate(tbNewbeeMallCarousel);
    }

}
